package com.wangzhen.codebase.apidemo;

import java.util.concurrent.atomic.AtomicReference;

public class AtomicReferenceDemo {
    class WMRange {
        final int upper;
        final int lower;

        WMRange(int upper, int lower) {
            this.upper = upper;
            this.lower = lower;
        }
    }

    final AtomicReference<WMRange> reference = new AtomicReference<>(new WMRange(0, 0));

    void setUpper(int v) {
        while (true) {
            WMRange wmRange = reference.get();
            if (v < wmRange.lower) {
                throw new IllegalArgumentException();
            }
            WMRange newRange = new WMRange(v, wmRange.lower);
            if (reference.compareAndSet(wmRange, newRange)) {
                break;
            }
        }
    }




}
